#pragma once
#include <iostream>
#include <pthread.h>
#include "mutex.hpp"

namespace CondModule
{
    using namespace MutexModule;
    class cond
    {
    public:
        cond()
        {
            int n = pthread_cond_init(&_cond, nullptr);
            if(n != 0) std::cerr << "create cond fail" << std::endl;
        }

        void notify()
        {
            int n = pthread_cond_signal(&_cond);
            if(n != 0) std::cerr << "notify cond fail" << std::endl;
        }

        void allnotify()
        {
            int n = pthread_cond_broadcast(&_cond);
            if(n != 0) std::cerr << "all notify cond fail" << std::endl;
        }

        void wait(mutex& lock)
        {
            int n = pthread_cond_wait(&_cond, lock.getlock());
            if(n != 0) std::cerr << "wait cond fail" << std::endl;
        }

        
        ~cond()
        {
            int n = pthread_cond_destroy(&_cond);
            if(n != 0) std::cerr << "destroy fail" << std::endl;
        }
    private:
        pthread_cond_t _cond;
    };
}